package com.superhelp.ebill.activity.login.presenter;


import com.superhelp.ebill.activity.login.view.LoginView;
import com.superhelp.ebill.api.ApiConfig;
import com.superhelp.ebill.api.HttpRequest;
import com.superhelp.ebill.api.RepuestParam;
import com.superhelp.ebill.api.result.LoginResult;
import com.superhelp.ebill.api.service.LoginService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/9/29.
 */

public class LoginPresenterImplementation implements LoginPresenter {


    private LoginView userLoginView;

    public LoginPresenterImplementation(LoginView userLoginView) {
        this.userLoginView = userLoginView;
    }

    @Override
    public void login(String userName, String passWord) {
        userLoginView.showLoading();
        RepuestParam params = new RepuestParam();

        params.setPhone(userName);
        params.setPwd(passWord);
        HttpRequest.getInstance()
                .create(LoginService.class, ApiConfig.BASE_URL)
                .login(params.getParams())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<LoginResult>() {
                    @Override
                    public void accept(LoginResult loginResult) throws Exception {
                        userLoginView.hideLoading();
                        userLoginView.onHttpSuccess(loginResult);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        userLoginView.hideLoading();
                        userLoginView.onHttpError(throwable);
                    }
                });


    }
}
